/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package janelas;

import Dao.InformacaoDAO;
import Modelo.Informacao;
import UTIL.ManipularImagem;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Roger
 */
public class Notebook extends javax.swing.JFrame {
    
    List<Informacao> lista = new ArrayList<Informacao>();

    Integer posicaoLista;
    //imagem carregada
    BufferedImage imagem;
    
    
    public Notebook(){
        initComponents();
        
        InformacaoDAO dao = new InformacaoDAO();
        lista = dao.Listar();
        imagem=null;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnPrimeiro = new javax.swing.JButton();
        btnUltimo = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        btnProximo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnInserir = new javax.swing.JButton();
        btnRemover = new javax.swing.JButton();
        btnAtualizar = new javax.swing.JButton();
        btnConsultar = new javax.swing.JButton();
        btnNovo = new javax.swing.JButton();
        lblNome = new javax.swing.JLabel();
        lblDescricao = new javax.swing.JLabel();
        lblPreco = new javax.swing.JLabel();
        lblPrecoBF = new javax.swing.JLabel();
        lblMarca = new javax.swing.JLabel();
        txtPrecoBF = new javax.swing.JTextField();
        txtPreco = new javax.swing.JTextField();
        txtDescricao = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtMarca = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        lblImagemF = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        lblImagemL = new javax.swing.JLabel();
        lblDestaque = new javax.swing.JLabel();
        lblCod = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        btnImagemf = new javax.swing.JButton();
        btnImageml = new javax.swing.JButton();
        cbxDestaque = new javax.swing.JComboBox();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jButton1.setText("jButton1");

        jButton2.setText("jButton2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Navegação"));

        btnPrimeiro.setText("Primeiro");
        btnPrimeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeiroActionPerformed(evt);
            }
        });

        btnUltimo.setText("Último");
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });

        btnAnterior.setText("Anterior");
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        btnProximo.setText("Próximo");
        btnProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProximoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnPrimeiro)
                .addGap(18, 18, 18)
                .addComponent(btnUltimo)
                .addGap(18, 18, 18)
                .addComponent(btnAnterior)
                .addGap(18, 18, 18)
                .addComponent(btnProximo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrimeiro)
                    .addComponent(btnUltimo)
                    .addComponent(btnAnterior)
                    .addComponent(btnProximo))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Operação"));

        btnInserir.setText("Inserir");
        btnInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInserirActionPerformed(evt);
            }
        });

        btnRemover.setText("Remover");
        btnRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverActionPerformed(evt);
            }
        });

        btnAtualizar.setText("Atualizar");
        btnAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtualizarActionPerformed(evt);
            }
        });

        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnInserir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnRemover)
                .addGap(18, 18, 18)
                .addComponent(btnAtualizar)
                .addGap(18, 18, 18)
                .addComponent(btnConsultar)
                .addGap(18, 18, 18)
                .addComponent(btnNovo)
                .addContainerGap(148, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInserir)
                    .addComponent(btnRemover)
                    .addComponent(btnAtualizar)
                    .addComponent(btnConsultar)
                    .addComponent(btnNovo))
                .addGap(12, 12, 12))
        );

        lblNome.setText("Nome: ");

        lblDescricao.setText("Descrição: ");

        lblPreco.setText("Preço Normal: ");

        lblPrecoBF.setText("Preço Black Friday: ");

        lblMarca.setText("Marca: ");

        txtMarca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMarcaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(lblImagemF)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(lblImagemF)
                .addGap(0, 86, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(lblImagemL)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(lblImagemL)
                .addGap(0, 86, Short.MAX_VALUE))
        );

        lblDestaque.setText("Destaque: ");

        lblCod.setText("Codigo do Produto: ");

        txtCodigo.setEditable(false);

        btnImagemf.setText("Selecionar Imagem de frente");
        btnImagemf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImagemfActionPerformed(evt);
            }
        });

        btnImageml.setText("Selecionar Imagem lateral");
        btnImageml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImagemlActionPerformed(evt);
            }
        });

        cbxDestaque.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sim", "Não" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPrecoBF)
                            .addComponent(lblPreco)
                            .addComponent(lblDescricao)
                            .addComponent(lblNome)
                            .addComponent(lblMarca)
                            .addComponent(lblDestaque)
                            .addComponent(lblCod))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPrecoBF)
                            .addComponent(txtNome)
                            .addComponent(txtDescricao)
                            .addComponent(txtPreco)
                            .addComponent(txtMarca, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(txtCodigo)
                            .addComponent(cbxDestaque, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnImagemf, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnImageml, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNome)
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnImagemf))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDescricao)
                            .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPreco)
                            .addComponent(txtPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPrecoBF)
                            .addComponent(txtPrecoBF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblMarca)
                            .addComponent(txtMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnImageml)
                                .addComponent(cbxDestaque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblDestaque))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCod)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtMarcaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMarcaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMarcaActionPerformed

    private void btnPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeiroActionPerformed
        Informacao jogo = lista.get(0);
        txtCodigo.setText(jogo.getCodigo().toString());
        txtNome.setText(jogo.getNome());
        txtPreco.setText(jogo.getPreco().toString());
        txtPrecoBF.setText(jogo.getPrecoBF().toString());
        txtDescricao.setText(jogo.getDescricao());
        txtMarca.setText(jogo.getCodmarca().toString());
        
        if(jogo.getDestaque() == true){
            cbxDestaque.setSelectedIndex(0);
        }else{
            cbxDestaque.setSelectedIndex(1);
        }

        
         //Chama a exibição da imagem
         exibeImagem(jogo.getImagemF());
         exibeImagem(jogo.getImagemL());
        posicaoLista = 0;
    }//GEN-LAST:event_btnPrimeiroActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        Informacao jogo = lista.get(lista.size() - 1);

        txtCodigo.setText(jogo.getCodigo().toString());
        txtNome.setText(jogo.getNome());
        txtDescricao.setText(jogo.getDescricao());
        txtMarca.setText(jogo.getCodmarca().toString());
        txtPreco.setText(jogo.getPreco().toString());
        txtPrecoBF.setText(jogo.getPrecoBF().toString());
        if(jogo.getDestaque() == true){
            cbxDestaque.setSelectedIndex(0);
        }else{
            cbxDestaque.setSelectedIndex(1);
        }
        
        
        //Chama a exibição da imagem
        exibeImagem(jogo.getImagemF());
        exibeImagem(jogo.getImagemL());
        posicaoLista = lista.size() - 1;
    }//GEN-LAST:event_btnUltimoActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        if (posicaoLista > 0) {
            posicaoLista = posicaoLista - 1;
            Informacao jogo = lista.get(posicaoLista);
            txtCodigo.setText(jogo.getCodigo().toString());
        txtNome.setText(jogo.getNome());
        txtDescricao.setText(jogo.getDescricao());
        txtMarca.setText(jogo.getCodmarca().toString());
        txtPreco.setText(jogo.getPreco().toString());
        txtPrecoBF.setText(jogo.getPrecoBF().toString());
        if(jogo.getDestaque() == true){
            cbxDestaque.setSelectedIndex(0);
        }else{
            cbxDestaque.setSelectedIndex(1);
        }
             //Chama a exibição da imagem
             exibeImagem(jogo.getImagemF());
             exibeImagem(jogo.getImagemL());

        }
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProximoActionPerformed
        if (posicaoLista < (lista.size() - 1)) {
            posicaoLista = posicaoLista + 1;
            Informacao jogo = lista.get(posicaoLista);
            txtCodigo.setText(jogo.getCodigo().toString());
        txtNome.setText(jogo.getNome());
        txtDescricao.setText(jogo.getDescricao());
        txtMarca.setText(jogo.getCodmarca().toString());
        txtPreco.setText(jogo.getPreco().toString());
        txtPrecoBF.setText(jogo.getPrecoBF().toString());
        if(jogo.getDestaque() == true){
            cbxDestaque.setSelectedIndex(0);
        }else{
            cbxDestaque.setSelectedIndex(1);
        }
             //Chama a exibição da imagem
            exibeImagem(jogo.getImagemF());
            exibeImagem(jogo.getImagemL());

        }
    }//GEN-LAST:event_btnProximoActionPerformed

    private void btnInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInserirActionPerformed
        Informacao jogo = new Informacao();
        boolean destaque = false;
        
        jogo.setNome(txtNome.getText());
        jogo.setDescricao(txtDescricao.getText());
        jogo.setCodmarca(Integer.parseInt(txtMarca.getText()));
        jogo.setPreco(Double.parseDouble(txtPreco.getText()));
        jogo.setPrecoBF(Double.parseDouble(txtPrecoBF.getText()));
        if(cbxDestaque.getSelectedIndex() == 0){
            destaque = true;
        }
        jogo.setDestaque(destaque);
        //INSERE A IMAGEM CONVERTIDA PARA BYTES
        jogo.setImagemF(ManipularImagem.getImgBytes(imagem));
        jogo.setImagemL(ManipularImagem.getImgBytes(imagem));
        //Acessa a DAO 
        InformacaoDAO dao = new InformacaoDAO();
        //CADASTRA NO BANCO DE DADOS
        dao.Cadastrar(jogo);

            //recarrega a lista
        JOptionPane.showMessageDialog(null, "Inserido com sucesso");

        limparDados();

        //atualizo a lista
        lista = dao.Listar();
    }//GEN-LAST:event_btnInserirActionPerformed

    private void btnRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverActionPerformed
        int confirma = JOptionPane.showConfirmDialog(null, "Deseja excluir?");

        if (confirma == 0) {
            Informacao obj = new Informacao();
            //passo a chave primária
            obj.setCodigo(Integer.parseInt(txtCodigo.getText()));
            //Chamar o EXCLUIR DA DAO
            InformacaoDAO dao = new InformacaoDAO();
            dao.Excluir(obj);

            //RECARREGA A LISTA
            lista = dao.Listar();
            limparDados();
            JOptionPane.showMessageDialog(null, "Excluído com sucesso!");
        }
    }//GEN-LAST:event_btnRemoverActionPerformed

    private void btnAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtualizarActionPerformed
        String nome = txtNome.getText();

        
        for (Informacao n : lista) {
            if (nome.equals(n.getNome())) { 
            
            txtCodigo.setText(n.getCodigo().toString());
            txtNome.setText(n.getNome());
            txtDescricao.setText(n.getDescricao());
            txtPreco.setText(n.getPreco().toString());
            txtPrecoBF.setText(n.getPrecoBF().toString());
            cbxDestaque.setSelectedItem(n.getDestaque().toString());
            txtMarca.setText(n.getCodmarca().toString());
            exibeImagem(n.getImagemF());
            exibeImagem(n.getImagemL());
            n.setImagemF(ManipularImagem.getImgBytes(imagem));
            n.setImagemL(ManipularImagem.getImgBytes(imagem));
            
            JOptionPane.showMessageDialog(null, "Notebook atualizado com sucesso");
            limparDados();
            break;
            
            } else {
                JOptionPane.showMessageDialog(null, "Você não pode modificar o nome do Notebook!");
            }
        }
        
       
    }//GEN-LAST:event_btnAtualizarActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
         String consulta
                = JOptionPane.showInputDialog("Informe o nome a ser"
                        + " consultado ");

        InformacaoDAO dao = new InformacaoDAO();
        List<Informacao> lista2 = new ArrayList<Informacao>();
        lista2 = dao.Listar(consulta);

        if (lista2.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Não encontrou nenhum registro");
        } else {
            Informacao i = lista2.get(0);
            txtCodigo.setText(i.getCodigo().toString());
        txtNome.setText(i.getNome());
        txtDescricao.setText(i.getDescricao());
        txtMarca.setText(i.getCodmarca().toString());
        cbxDestaque.setSelectedItem(i.getDestaque().toString());
        txtPreco.setText(i.getPreco().toString());
        txtPrecoBF.setText(i.getPrecoBF().toString());
        
             //Chama a exibição da imagem
            exibeImagem(i.getImagemF());
            exibeImagem(i.getImagemL());
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        limparDados();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnImagemfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImagemfActionPerformed
         JFileChooser fc = new JFileChooser();
        int res = fc.showOpenDialog(null);

        if (res == JFileChooser.APPROVE_OPTION) {
            File arquivo = fc.getSelectedFile();
            
         
            
                try {
                    imagem = ManipularImagem.setImagemDimensao(arquivo.getAbsolutePath(), 160, 160);
                    
                    lblImagemF.setIcon(new ImageIcon(imagem));
                    
                } catch (Exception ex) {
                    Logger.getLogger(Notebook.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            
        } else {
            JOptionPane.showMessageDialog(null, "Voce nao selecionou nenhum arquivo.");
        }
    }//GEN-LAST:event_btnImagemfActionPerformed

    private void btnImagemlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImagemlActionPerformed
         JFileChooser fc = new JFileChooser();
        int res = fc.showOpenDialog(null);

        if (res == JFileChooser.APPROVE_OPTION) {
            File arquivo = fc.getSelectedFile();
            
         
            
                try {
                    imagem = ManipularImagem.setImagemDimensao(arquivo.getAbsolutePath(), 160, 160);
                    
                    lblImagemL.setIcon(new ImageIcon(imagem));
                    
                } catch (Exception ex) {
                    Logger.getLogger(Notebook.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            
        } else {
            JOptionPane.showMessageDialog(null, "Voce nao selecionou nenhum arquivo.");
        }
    }//GEN-LAST:event_btnImagemlActionPerformed
    private void exibeImagem(byte[] minhaimagem)
{
        //primeiro verifica se tem a imagem
        //se tem convert para inputstream que é o formato reconhecido pelo ImageIO
       
        if(minhaimagem!=null)
        {
            InputStream input = new ByteArrayInputStream(minhaimagem);
            try {
                imagem = ImageIO.read(input);
                
            } catch (IOException ex) {
                Logger.getLogger(Notebook.class.getName()).log(Level.SEVERE, null, ex);
            }
            lblImagemF.setIcon(new ImageIcon(imagem));
            lblImagemL.setIcon(new ImageIcon(imagem));
        
        }
        else
        {
            lblImagemF.setIcon(null);
            lblImagemL.setIcon(null);
            imagem=null;
        }

}
    public void limparDados() {
        txtCodigo.setText("");
        txtNome.setText("");
        txtDescricao.setText("");
        txtMarca.setText("");
        cbxDestaque.setSelectedIndex(-1);
        txtPreco.setText("");
        txtPrecoBF.setText("");
        lblImagemF.setIcon(null);
        lblImagemL.setIcon(null);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Notebook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Notebook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Notebook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Notebook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
              new Notebook().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnAtualizar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnImagemf;
    private javax.swing.JButton btnImageml;
    private javax.swing.JButton btnInserir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnPrimeiro;
    private javax.swing.JButton btnProximo;
    private javax.swing.JButton btnRemover;
    private javax.swing.JButton btnUltimo;
    private javax.swing.JComboBox cbxDestaque;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblCod;
    private javax.swing.JLabel lblDescricao;
    private javax.swing.JLabel lblDestaque;
    private javax.swing.JLabel lblImagemF;
    private javax.swing.JLabel lblImagemL;
    private javax.swing.JLabel lblMarca;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblPreco;
    private javax.swing.JLabel lblPrecoBF;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescricao;
    private javax.swing.JTextField txtMarca;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtPreco;
    private javax.swing.JTextField txtPrecoBF;
    // End of variables declaration//GEN-END:variables
}
